package com.have.blog.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.have.blog.admin.domain.dto.FeedbackDto;
import com.have.blog.admin.domain.dto.UserDto;
import com.have.blog.admin.service.FeedbackService;
import com.have.blog.admin.utils.SecurityUtil;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.UserFeedbackMapper;
import com.have.blog.model.mapper.UserMapper;
import com.have.blog.model.models.User;
import com.have.blog.model.models.UserExample;
import com.have.blog.model.models.UserFeedback;
import com.have.blog.model.models.UserFeedbackExample;
import com.have.blog.utils.MyBeanUtils;
import com.have.blog.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
@SuppressWarnings("all")
public class FeedbackServiceImpl implements FeedbackService {
    @Autowired
    private UserFeedbackMapper feedbackMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Wrapper<CommonPage<FeedbackDto>> getPageList(FeedbackDto feedbackDto) {
        Page page = PageHelper.startPage(feedbackDto.getPageNum(), feedbackDto.getPageSize());
        UserFeedbackExample example = new UserFeedbackExample();
        UserFeedbackExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(feedbackDto.getTitle())) {
            criteria.andTitleLike("%" + feedbackDto.getKeyword() + "%");
        }
        if (feedbackDto.getFeedbackStatus() != null) {
            criteria.andFeedbackStatusEqualTo(feedbackDto.getFeedbackStatus());
        }
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        example.setOrderByClause("create_time desc");
        List<UserFeedback> userFeedbacks = feedbackMapper.selectByExample(example);
        List<FeedbackDto> feedbackDtos = MyBeanUtils.copyListProperties(userFeedbacks, FeedbackDto::new);
        CommonPage<FeedbackDto> result = CommonPage.restPage(feedbackDtos);
        result.setTotal(page.getTotal());
        result.setPageNum(page.getPageNum());
        result.setPageSize(page.getPageSize());

        List<Integer> userUids = new ArrayList<>();
        for (FeedbackDto dto : feedbackDtos) {
            if (dto.getUserUid() != null) {
                userUids.add(dto.getUserUid());
            }
        }
        if (!CollectionUtils.isEmpty(userUids)) {
            UserExample userExample = new UserExample();
            userExample.createCriteria()
                    .andUidIn(userUids);
            List<User> userList = userMapper.selectByExample(userExample);
            List<UserDto> userDtos = MyBeanUtils.copyListProperties(userList, UserDto::new);
            Map<Integer, UserDto> userMap = new HashMap<>();
            for (UserDto user : userDtos) {
                userMap.put(user.getUid(), user);
            }
            for (FeedbackDto dto : feedbackDtos) {
                dto.setUser(userMap.get(dto.getUserUid()));
            }
        }
        return Wrapper.success(result);
    }

    @Transactional
    @Override
    public Wrapper editFeedback(FeedbackDto feedbackDto) {
        UserFeedback userFeedback = feedbackMapper.selectByPrimaryKey(feedbackDto.getUid());
        Integer adminUid = SecurityUtil.getUserId();
        userFeedback.setAdminUid(adminUid);
        userFeedback.setFeedbackStatus(feedbackDto.getFeedbackStatus());
        userFeedback.setReply(feedbackDto.getReply());
        userFeedback.setUpdateTime(new Date());
        int update = feedbackMapper.updateByPrimaryKeySelective(userFeedback);
        if (update > 0) {
            return Wrapper.success();
        }
        return Wrapper.failed();
    }

    @Transactional
    @Override
    public Wrapper deleteBatchFeedback(List<Integer> uids) {
        if (CollectionUtils.isEmpty(uids)) {
            return Wrapper.illegal();
        }
        UserFeedbackExample example = new UserFeedbackExample();
        example.createCriteria()
                .andUidIn(uids);
        UserFeedback userFeedback = new UserFeedback();
        userFeedback.setStatus(BlogStatus.DISABLE.getStatus());
        int update = feedbackMapper.updateByExampleSelective(userFeedback, example);
        if (update > 0) {
            return Wrapper.success();
        }
        return Wrapper.failed();
    }
}
